/******************************************************************************
秋夜静，独坐对残灯。
啼笑非非谁识我，坐行梦梦尽缘君，何所慰消沉。
风卷雨，雨复卷侬心。
心似欲随风雨去，茫茫大海任浮沉。
无爱亦无恨。
******************************************************************************/
#include "kbRGB_Angle.h"
#include <stdio.h>
#include <math.h>

static const uint8_t SinTable[182] = {
0x00, 0x02, 0x04, 0x06, 0x08, 0x0B, 0x0D, 0x0F, 0x11, 0x14, 0x16, 0x18, 0x1A, 0x1C, 0x1F, 0x21, 0x23, 0x25, 0x27, 0x2A,
0x2C, 0x2E, 0x30, 0x32, 0x35, 0x37, 0x39, 0x3B, 0x3D, 0x3F, 0x41, 0x44, 0x46, 0x48, 0x4A, 0x4C, 0x4E, 0x50, 0x53, 0x55,
0x57, 0x59, 0x5B, 0x5D, 0x5F, 0x61, 0x63, 0x65, 0x67, 0x69, 0x6B, 0x6D, 0x6F, 0x71, 0x73, 0x75, 0x77, 0x79, 0x7B, 0x7D,
0x7F, 0x81, 0x83, 0x85, 0x87, 0x89, 0x8A, 0x8C, 0x8E, 0x90, 0x92, 0x94, 0x95, 0x97, 0x99, 0x9B, 0x9C, 0x9E, 0xA0, 0xA2,
0xA3, 0xA5, 0xA7, 0xA8, 0xAA, 0xAC, 0xAD, 0xAF, 0xB1, 0xB2, 0xB4, 0xB5, 0xB7, 0xB8, 0xBA, 0xBC, 0xBD, 0xBE, 0xC0, 0xC1,
0xC3, 0xC4, 0xC6, 0xC7, 0xC8, 0xCA, 0xCB, 0xCC, 0xCE, 0xCF, 0xD0, 0xD2, 0xD3, 0xD4, 0xD5, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB,
0xDC, 0xDD, 0xDF, 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEE,
0xEF, 0xF0, 0xF1, 0xF1, 0xF2, 0xF3, 0xF3, 0xF4, 0xF5, 0xF5, 0xF6, 0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x58, 0x56, 0x54, 0x51, 0x4F, 0x4D, 0x4B, 0x49, 0x47, 0x45, 0x42, 0x40, 0x3E, 0x3C, 0x3A, 0x38, 0x36, 0x33, 0x31, 0x2F,
0x2D, 0x2B, 0x28, 0x26, 0x24, 0x22, 0x20, 0x1D, 0x1B, 0x19, 0x17, 0x15, 0x12, 0x10, 0x0E, 0x0C, 0x09, 0x07, 0x05, 0x03,
0x00, 0xFF,
};

const int16_t kbRGB_SinMax = ((uint16_t)0x1<<(KBRGB_SIN_CAL_PRE))-1;

static uint16_t _kbRGB_Angle(float x, float y, float cx, float cy)
{
    double angle    = (atan2(x - cx, y - cy)) * 180 / 3.14159265359;
    double distance = sqrt(pow(cx - x, 2) + pow(cy - y, 2));

    struct kbRGB_PixelLimit piont;
#if 0
    piont.angle = (uint16_t)(angle*8.0f);     // 90*8 = 720:10bit[1024]
    piont.radius = (uint16_t)(distance);
    piont.radius = (uint16_t)(distance*2.0); // sqrt(21*21)*2 = 29.7*2 = 60: 6bit [64]
#else
    const float kbRGB_angls_pre = (float)(0x1<<KBRGB_ANGLE_PRE);
    const float kbRGB_radius_pre = (float)(0x1<<KBRGB_RADIUS_PRE);
    piont.angle = (uint16_t)(angle*kbRGB_angls_pre);     // 90*8 = 720:10bit[1024]
    piont.radius = (uint16_t)(distance*kbRGB_radius_pre); // sqrt(21*21)*2 = 29.7*2 = 60: 6bit [64]
#endif
    return piont.pixel;
}
/*************************************************************************************************
* 为节省空间占用,仅计算 0-90°范围,其它角度根据象限间的关系重新计算
* 为节保留 精度并加快运算速度,将处理后的结果保存为整型，角度精度为 1/2, 结果精度为 1/255
**************************************************************************************************/
void kbRGB_Sin_Cal(void)
{
    const double pre = (double)(1<<KBRGB_SIN_PRE);  // 弧度
    const uint32_t AngleMax = (90+1)<<KBRGB_SIN_PRE;
    const double PI = 3.141592654f;
    uint32_t angle = 0;
    double rad = 0;  // 弧度
    double _sin=0;
    uint8_t sin_table[360*8];
    for(angle = 0; angle < AngleMax; angle++)
    {
        //rad = (angle/pre)*PI/(180.0f);  // 弧度
        rad = angle*PI/(pre*180.0f);  // 弧度
        _sin=sin(rad);
        _sin = _sin*255;
        if(_sin>255) _sin=255.1;
        sin_table[angle] = _sin;
        //if((sin_table[angle]>0) && (sin_table[angle]<255)) sin_table[angle]+=1;
    }
    sin_table[((90)<<KBRGB_SIN_PRE)] = 255;
    //printf("const uint16_t RotateMatrix[%d][%d] = {\r\n", MATRIX_SIZE, MATRIX_SIZE);
    //printf("const uint16_t RotateMatrixAngle[MATRIX_SIZE][MATRIX_SIZE] = {\r\n");
    //printf("const uint32_t RotateMatrixAngle[%d][%d] = {\r\n", cols, rows);
    printf("static const uint8_t SinTable[%d] = {\r\n", AngleMax);
    for(angle = 0; angle < AngleMax; angle++)
    {
        printf("0x%02X, ",  sin_table[angle]);
        if((angle>0) && (19==(angle%20))) printf("\r\n");
    }
    printf("\r\n};\r\n");
    fflush(stdout);
}
void kbRGB_Cos_Cal(void)
{
    const double pre = (double)(1<<KBRGB_SIN_PRE);  // 弧度
    const uint32_t AngleMax = (90+1)<<KBRGB_SIN_PRE;
    const double PI = 3.141592654f;
    uint32_t angle = 0;
    double rad = 0;  // 弧度
    double _cos=0;
    uint8_t cos_table[360*8];
    for(angle = 0; angle < AngleMax; angle++)
    {
        //rad = (angle/pre)*PI/(180.0f);  // 弧度
        rad = angle*PI/(pre*180.0f);  // 弧度
        _cos=cos(rad);
        _cos = _cos*255;
        if(_cos>255) _cos=255.1;
        cos_table[angle] = _cos;
        if((cos_table[angle]>0) && (cos_table[angle]<255)) cos_table[angle]+=1;
    }
    cos_table[((90)<<KBRGB_SIN_PRE)] = 0;
    //printf("const uint16_t RotateMatrix[%d][%d] = {\r\n", MATRIX_SIZE, MATRIX_SIZE);
    //printf("const uint16_t RotateMatrixAngle[MATRIX_SIZE][MATRIX_SIZE] = {\r\n");
    //printf("const uint32_t RotateMatrixAngle[%d][%d] = {\r\n", cols, rows);
    printf("static const uint8_t CosTable[%d] = {\r\n", AngleMax);
    for(angle = 0; angle < AngleMax; angle++)
    {
        printf("0x%02X, ",  cos_table[angle]);
        if((angle>0) && (19==(angle%20))) printf("\r\n");
    }
    printf("\r\n};\r\n");
    fflush(stdout);
}
//#define CircleAngle (360<<KBRGB_SIN_PRE)
int16_t kbRGB_Sin(const float angle)
{
    int32_t Angle = angle*(1<<KBRGB_SIN_PRE);
    int16_t _sin=0;
    Angle = Angle%(360<<KBRGB_SIN_PRE);
    while(Angle<0) Angle = Angle+(360<<KBRGB_SIN_PRE);
    //while(Angle>=(360<<KBRGB_SIN_PRE)) Angle = Angle-(360<<KBRGB_SIN_PRE);
    if(Angle<=(90<<KBRGB_SIN_PRE)) // 第一象限
    {
        _sin = SinTable[Angle];
    }
    else if(Angle<=(180<<KBRGB_SIN_PRE)) // 第二象限
    {
        _sin = SinTable[(180<<KBRGB_SIN_PRE)-Angle];
    }
    else if(Angle<=(270<<KBRGB_SIN_PRE)) // 第三象限
    {
        _sin = SinTable[Angle-(180<<KBRGB_SIN_PRE)];
        _sin = -_sin;
    }
    else //if(Angle<=(360<<KBRGB_SIN_PRE)) // 第四象限
    {
        _sin = SinTable[(360<<KBRGB_SIN_PRE)-Angle];
        _sin = -_sin;
    }
    return _sin;
}

int16_t kbRGB_Sin_Int(const int16_t angle)
{
    int32_t Angle = angle;
    int16_t _sin=0;
    Angle = Angle%(360<<KBRGB_SIN_PRE);
    while(Angle<0) Angle = Angle+(360<<KBRGB_SIN_PRE);
    //while(Angle>=(360<<KBRGB_SIN_PRE)) Angle = Angle-(360<<KBRGB_SIN_PRE);
    if(Angle<=(90<<KBRGB_SIN_PRE)) // 第一象限
    {
        _sin = SinTable[Angle];
    }
    else if(Angle<=(180<<KBRGB_SIN_PRE)) // 第二象限
    {
        Angle = (180<<KBRGB_SIN_PRE)-Angle;
        //_sin = SinTable[(180<<KBRGB_SIN_PRE)-Angle];
        _sin = SinTable[Angle];
    }
    else if(Angle<=(270<<KBRGB_SIN_PRE)) // 第三象限
    {
        Angle = Angle-(180<<KBRGB_SIN_PRE);
        //_sin = SinTable[Angle-(180<<KBRGB_SIN_PRE)];
        _sin = SinTable[Angle];
        _sin = -_sin;
    }
    else //if(Angle<=(360<<KBRGB_SIN_PRE)) // 第四象限
    {
        Angle = (360<<KBRGB_SIN_PRE)-Angle;
        //_sin = SinTable[(360<<KBRGB_SIN_PRE)-Angle];
        _sin = SinTable[Angle];
        _sin = -_sin;
    }
    return _sin;
}

int16_t kbRGB_Sin_Int_Lin(const int16_t angle)
{
    int32_t Angle = angle;
    int16_t _sin=0;
    Angle = Angle%(360<<KBRGB_SIN_PRE);
    while(Angle<0) Angle = Angle+(360<<KBRGB_SIN_PRE);
    //while(Angle>=(360<<KBRGB_SIN_PRE)) Angle = Angle-(360<<KBRGB_SIN_PRE);
    if(Angle<=(90<<KBRGB_SIN_PRE)) // 第一象限
    {
        _sin = Angle*255/(90<<KBRGB_SIN_PRE);
    }
    else if(Angle<=(180<<KBRGB_SIN_PRE)) // 第二象限
    {
        Angle = (180<<KBRGB_SIN_PRE)-Angle;
        _sin = Angle*255/(90<<KBRGB_SIN_PRE);
    }
    else if(Angle<=(270<<KBRGB_SIN_PRE)) // 第三象限
    {
        Angle = Angle-(180<<KBRGB_SIN_PRE);
        _sin = Angle*255/(90<<KBRGB_SIN_PRE);
        _sin = -_sin;
    }
    else //if(Angle<=(360<<KBRGB_SIN_PRE)) // 第四象限
    {
        Angle = (360<<KBRGB_SIN_PRE)-Angle;
        _sin = Angle*255/(90<<KBRGB_SIN_PRE);
        _sin = -_sin;
    }
    return _sin;
}

#if 0
int16_t kbRGB_Cos(const int16_t angle)
{
    int16_t Angle = angle;
    int16_t _cos=0;
    while(Angle<0) Angle = Angle+(360<<KBRGB_SIN_PRE);
    while(Angle>=(360<<KBRGB_SIN_PRE)) Angle = Angle-(360<<KBRGB_SIN_PRE);
    if(Angle<=(90<<KBRGB_SIN_PRE)) // 第一象限
    {
        _cos = CosTable[Angle];
    }
    else if(Angle<=(180<<KBRGB_SIN_PRE)) // 第二象限
    {
        _cos = CosTable[(180<<KBRGB_SIN_PRE)-Angle];
        _cos = -_cos;
    }
    else if(Angle<=(270<<KBRGB_SIN_PRE)) // 第三象限
    {
        _cos = CosTable[Angle-(180<<KBRGB_SIN_PRE)];
        _cos = -_cos;
    }
    else //if(Angle<=(360<<KBRGB_SIN_PRE)) // 第四象限
    {
        _cos = CosTable[(360<<KBRGB_SIN_PRE)-Angle];
    }
    return _cos;
}
#else
// 采用内联函数
//int16_t kbRGB_Cos(const float angle)
//{
//    return kbRGB_Sin(angle+90.0f);
//}
#endif
